package com.cn.mybatis;

import com.cn.mybatis.dao.IOrderDao;
import com.cn.mybatis.dao.IRoleDao;
import com.cn.mybatis.dao.IUserDao;
import com.cn.mybatis.pojo.OrderBean;
import com.cn.mybatis.pojo.RoleBean;
import com.cn.mybatis.pojo.UserBean;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import org.omg.CORBA.IRObject;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class CacheTest {

    private SqlSession sqlSession;

    @Before
    public void before(){
        InputStream resourceAsStream = null;
        try {
            resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
            this.sqlSession = sqlSessionFactory.openSession(true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void firstLevelCache(){
        IUserDao mapper = sqlSession.getMapper(IUserDao.class);
        List<UserBean> list1 = mapper.getList();

        List<UserBean> list2 = mapper.getList();

        System.out.println(list1 == list2);
    }

    @Test
    public void secondLevelCache() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession1 = sqlSessionFactory.openSession(true);
        SqlSession sqlSession2 = sqlSessionFactory.openSession(true);
        SqlSession sqlSession3 = sqlSessionFactory.openSession(true);

        IUserDao mapper1 = sqlSession1.getMapper(IUserDao.class);
        IUserDao mapper2 = sqlSession2.getMapper(IUserDao.class);
        IUserDao mapper3 = sqlSession3.getMapper(IUserDao.class);

        List<UserBean> list1 = mapper1.getList();
        sqlSession1.close();// 清空一级缓存 , 生成二级缓存
        List<UserBean> list2 = mapper2.getList();

        System.out.println(list1 == list2);
    }


    @Test
    public void redisCache() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession1 = sqlSessionFactory.openSession(true);
        SqlSession sqlSession2 = sqlSessionFactory.openSession(true);
        SqlSession sqlSession3 = sqlSessionFactory.openSession(true);

        IOrderDao mapper1 = sqlSession1.getMapper(IOrderDao.class);
        IOrderDao mapper2 = sqlSession2.getMapper(IOrderDao.class);
        IOrderDao mapper3 = sqlSession3.getMapper(IOrderDao.class);

        List<OrderBean> oneToOne1 = mapper1.getOneToOne(1);
        sqlSession1.close();
        List<OrderBean> oneToOne2 = mapper2.getOneToOne(1);
        List<OrderBean> oneToOne3 = mapper3.getOneToOne(1);


        System.out.println(oneToOne1 == oneToOne2);
    }
}
